//
//  HOPNewFeatureController.swift
//  WB01
//
//  Created by HOP on 15/12/17.
//  Copyright © 2015年 HOP. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HOPNewFeatureController: UICollectionViewController {
     /// 布局属性
    let layout = UICollectionViewFlowLayout()
    init() {
    super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 新特性界面数量
    private let ItemCount = 4

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
        
    }
    // MARK: - 准备collectionView的相关参数
    private func prepareCollectionView() {
        //注册
        self.collectionView!.registerClass(HOPNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        // 设置布局参数
        // 一个item和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
    // 不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
    // 水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    // 分页显示
        collectionView?.pagingEnabled = true
        // 取消弹簧效果
    collectionView?.bounces = false
    
    
    }
    
   
        override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return ItemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        // 去缓存池中查找
        // as 类型转换, 但是有可能转成功,也有可能转失败
        // as! 表示一定能转成功,如果一旦转换失败,程序崩溃
        // as? 表示有可能转成功,也有可能转不成功, 返回值是一个可选
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HOPNewFeatureCell
    
        cell.index = indexPath.item
    
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
//        print("indexPath: \(indexPath)")
        // collectionView停到某一页,不使用indexPath,我们自己来获取现在正在显示的cell indexPath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
//        print("正在显示cell的indexPath: \(visibleIndexPath)")
        if visibleIndexPath.item == ItemCount - 1 {
        // 获取visibleIndexPath对应的cell
        let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! HOPNewFeatureCell
         // 调用cell开始动画
            cell.startButtonAnimation()
        }
    }
    
    
    
    
class HOPNewFeatureCell: UICollectionViewCell {
        // MARK: - 属性
        // 不同的cell显示不同的图片,别人告诉我们显示哪一页的图片
        // 根据不同的index来设置不同的图片
        var index = 0 {
        // 属性监视器, 当index发生变化时会调用
            didSet {
            bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            startButton.hidden = true
             }
         }
    
// MARK: - 开始按钮动画
    func startButtonAnimation() {
    // 显示按钮
    startButton.hidden = false
    // 设置开始按钮的scale
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        // 使用UIView动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 将开始按钮的transform的缩放设置成1(x,y)
            //            self.startButton.transform = CGAffineTransformMakeScale(1, 1)
            // 清空transform
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("开始按钮动画完成")
        }
    
    }
          // 构造函数
        override init(frame: CGRect) {
            super.init(frame: frame)
            backgroundColor = UIColor.whiteColor()
            prepareUI()
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    
        // MARK: - 准备UI
        private func prepareUI() {
            // 添加为子控件
            contentView.addSubview(bkgImageView)
            contentView.addSubview(startButton)
            
//            bkgImageView.backgroundColor = UIColor.brownColor()
            // 添加约束
            bkgImageView.translatesAutoresizingMaskIntoConstraints = false
            startButton.translatesAutoresizingMaskIntoConstraints = false
            // 背景填充父控件
            contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
             contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
            contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
            // 开始体验按钮约束
            // 水平CenterX和父控件的CenterX重合
            contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            // 垂直开始按钮的底部和父控件的底部重合
            contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        }
    
    //背景图片
    private lazy var bkgImageView : UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
        /// 开始按钮
    private lazy var startButton: UIButton = {
    let button = UIButton()
    // 设置背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        // 设置按钮文字
        button.setTitle("开始体验", forState: UIControlState.Normal)
        // 因为cell复用,不能再创建的时候来隐藏cell
        //        button.hidden = true
        //添加点击事件
        button.addTarget(self, action: "BtnClikc", forControlEvents: UIControlEvents.TouchUpInside)
    return button
    }()
   // MARK: - 点击事件
    func BtnClikc() {
    // 调用AppDelegate来切换根控制器,不能直接创建AppDelegate(),需要使用系统维护的AppDelegate
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
     }
    }
    
}
























